package com.shaomengjie.smjmvp.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

import com.shaomengjie.smjmvp.R;
import com.shaomengjie.smjmvp.view.core.BaseActivity;
import com.shaomengjie.smjmvp.view.core.BaseFragment;
import com.shaomengjie.smjmvp.view.fragment.ClassfiyFragment;
import com.shaomengjie.smjmvp.view.fragment.HomeFragment;
import com.shaomengjie.smjmvp.view.fragment.MineFragment;
import com.shaomengjie.smjmvp.view.fragment.ShoppingCartFragment;

import java.util.ArrayList;

import me.majiajie.pagerbottomtabstrip.Controller;
import me.majiajie.pagerbottomtabstrip.PagerBottomTabLayout;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectListener;

public class MainActivity extends BaseActivity {
    private FrameLayout mFrameLayout;
    private PagerBottomTabLayout mTab;
    private Controller controller;
    private int currentIndex;
    private BaseFragment mCurrentFragment;
    private ArrayList<Class<? extends BaseFragment>> mFragmentClassData = new ArrayList<>();
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initView() {
        mFrameLayout = (FrameLayout) findViewById(R.id.mainActivity_framelayout);
        mTab = (PagerBottomTabLayout) findViewById(R.id.tab);
        mFragmentClassData.add(HomeFragment.class);
        mFragmentClassData.add(ClassfiyFragment.class);
        mFragmentClassData.add(ShoppingCartFragment.class);
        mFragmentClassData.add(MineFragment.class);
        initLister();
    }

    private void initLister() {
        controller = mTab.builder()
                .addTabItem(R.mipmap.ic_launcher,"首页")
                .addTabItem(R.mipmap.ic_launcher,"分类")
                .addTabItem(R.mipmap.ic_launcher,"购物车")
                .addTabItem(R.mipmap.ic_launcher,"我的")
                .build();
        controller.addTabItemClickListener(new OnTabItemSelectListener() {
            @Override
            public void onSelected(int index, Object tag) {
                try {
                    FragmentManager manager = getSupportFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    Fragment to = manager.findFragmentByTag(index+"");
                    Fragment from = manager.findFragmentByTag(currentIndex+"");
                    if (to==null){
                        to = mFragmentClassData.get(index).newInstance();
                        if (from == null){
                            transaction.add(R.id.mainActivity_framelayout,to,index+"").commitAllowingStateLoss();
                        }else {
                            transaction.hide(from).add(R.id.mainActivity_framelayout,to,index+"").commitAllowingStateLoss();
                        }
                    }else {
                        transaction.hide(from).show(to).commitAllowingStateLoss();
                    }
                    mCurrentFragment = (BaseFragment) to;
                    currentIndex = index;

                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onRepeatClick(int index, Object tag) {

            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }
}
